package com.coisini.service.impl;

import com.coisini.constant.MessageConstant;
import com.coisini.constant.StatusConstant;
import com.coisini.dto.SetmealDTO;
import com.coisini.dto.SetmealPageQueryDTO;
import com.coisini.entity.Dish;
import com.coisini.entity.Setmeal;
import com.coisini.entity.SetmealDish;
import com.coisini.exception.DeletionNotAllowedException;
import com.coisini.exception.SetmealEnableFailedException;
import com.coisini.mapper.DishMapper;
import com.coisini.mapper.SetmealDishMapper;
import com.coisini.mapper.SetmealMapper;
import com.coisini.result.PageResult;
import com.coisini.service.SetmealService;
import com.coisini.vo.DishItemVO;
import com.coisini.vo.SetmealVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 套餐业务
 */
@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    /**
     * 新增套餐
     * 1.在套餐表中新增数据，并获取该套餐的主键id
     * 2.在套餐-菜品表中还需要新增关系数据
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void save(SetmealDTO setmealDTO) {
        //1.新增套餐
        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //套餐的默认状态是停售状态
        setmeal.setStatus(StatusConstant.DISABLE);
        setmealMapper.insert(setmeal);

        //获得新增套餐的主键id
        Long setmealId = setmeal.getId();

        //2.新增套餐-菜品关系表数据
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        //遍历setmealDishes集合，获取每一个SetmealDish对象
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealId);
            setmealDishMapper.insert(setmealDish);
        }
    }

    /**
     * 套餐分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        //设置查询条件
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        //调用mapper接口查询
       List<SetmealVO> setmealList= setmealMapper.pageQuery(setmealPageQueryDTO);
        Page<SetmealVO> setmealPage= (Page<SetmealVO>) setmealList;
        return new PageResult(setmealPage.getTotal(),setmealPage.getResult());
    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @Override
    public SetmealVO getById(long id) {
        //先根据id查询套餐的信息
        SetmealVO setmealVO=new SetmealVO();
        Setmeal setmeal = setmealMapper.getById(id);
        //在根据套餐id查询套餐-菜品的信息
        List<SetmealDish> setmealDishes = setmealDishMapper.getBySetmealId(id);
        BeanUtils.copyProperties(setmeal,setmealVO);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    /**
     * 修改套餐
     * 修改套餐表，同时也需要修改套餐-菜品关系表
     * 修改套餐-菜品关系表，可以先将已有数据删除，在新增数据
     * @param setmealDTO
     */
    @Override
    public void update(SetmealDTO setmealDTO) {
        //1.根据套餐id，修改套餐表
        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.update(setmeal);

        //2.批量删除该套餐id对应的套餐-菜品关系数据
        setmealDishMapper.delete(setmealDTO.getId());

        //3.新增套餐-菜品关系
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealDTO.getId());
            setmealDishMapper.insert(setmealDish);
        }
    }

    /**
     * 套餐起售、禁售
     * 底层就是根据id，修改套餐表中的数据
     * 前提：还需要判断，包含的菜品的状态，当菜品中有停售状态的，该套餐只能停售
     * @param status
     * @param id
     */
    @Override
    public void changeStatus(Integer status, Long id) {
        //1.首先根据套餐id查询套餐-关系表，查询到菜品的id
        List<SetmealDish> setmealDishes = setmealDishMapper.getBySetmealId(id);
        List<Long> dishIdList=new ArrayList<>();
        setmealDishes.forEach(setmealDish -> dishIdList.add(setmealDish.getDishId()));
        if (dishIdList != null && dishIdList.size()>0){
            //根据菜品id查询菜品
            List<Dish> dishList = dishMapper.getByids(dishIdList);
            for (Dish dish : dishList) {
                if (dish.getStatus() == StatusConstant.DISABLE){
                    throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                }
            }
        }

        //2.根据套餐id修改套餐的数据
        Setmeal setmeal = Setmeal.builder().status(status).id(id).build();
        setmealMapper.update(setmeal);
    }

    /**
     * 批量删除套餐
     * 1.需要删除套餐表中数据
     * 2.同时也需要删除套餐-菜品表中数据
     *
     * *** 起售中的套餐不能删除
     * @param ids
     */
    @Override
    @Transactional
    public void delete(List<Long> ids) {
        //***注：起售中的套餐不能删除
        for (Long id : ids) {
            Setmeal setmeal = setmealMapper.getById(id);
            if (setmeal.getStatus()== StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }

        //1.批量删除套餐表中数据
        setmealMapper.delete(ids);

        //2.删除套餐-菜品关系表中的数据
        for (Long id : ids) {
            setmealDishMapper.delete(id);
        }
    }


    /**
     * 根据分类id查询套餐
     * @param setmeal
     * @return
     */
    @Override
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list= setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据套餐id查询对应的菜品
     * @param id
     * @return
     */
    @Override
    public List<DishItemVO> getDishItemById(long id) {
       return setmealMapper.getDishItemBySetmealId(id);
    }
}
